Game.prototype = new Sprite ();
Game.picturesPath = "pictures/";
Game._game = null;
Game.imagesLoaded = false;
Game.UTF8Encode = function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
 
    for (var n = 0; n < string.length; n++) {
 
        var c = string.charCodeAt(n);
 
        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
 
    }
 
    return utftext;
};

function Game (player){
    this.paths = 
    Sprite.apply(this);
    Game._game = this;
    this.score = new Score();
    this.levels = new Array();
    this.jokeLevels = new Array();
    this.levelOrder = ["levelCircuit","levelElements","levelFunctions"
    ,"levelLogic","levelOptic","levelPi","levelPythagoras"
    ,"levelSort", "levelTurtle"];
    
    this.menus = new Array();
    this.currentLevel = null;
    this.bitmaps = new Array();
    this.player = player;
    this.selectLevelMode = false;
    
    // methods
    this.StartGame = StartGame;
    this.LoadLevel = LoadLevel;
    this.LoadLevels = LoadLevels;
    this.LoadJokeLevels = LoadJokeLevels;
    this.LoadMenus = LoadMenus;
    this.LoadMainMenu = LoadMainMenu;
    this.GameInitialize = GameInitialize;
    this.StartLevel = StartLevel;
    this.NextLevel = NextLevel;
    this.RestartLevel = RestartLevel;
    this.LoadBitmaps = LoadBitmaps;
    
    function StartGame (){
        this.StartLevel (this.levels[this.levelOrder[0]]);
        this.currentLevel = 0;
    }
    
    function LoadLevel (name){
        switch(name){
            case "levelCircuit":
                this.levels[name] = new CircuitLevel (typePhys);
                this.levels[name].init();
                break;
            case "levelElements":
                this.levels[name] = new ElementsLevel (typePhys);
                this.levels[name].init();
        
                break;
            case "levelFunctions":
                this.levels[name] = new FunctionsLevel (typeMath);
                this.levels[name].init();
        
                break;
            case "levelLogic":
                this.levels[name] = new LogickeHodnotyLevel (typeInf);
                this.levels[name].init();
                break;
            case "levelOptic":
                this.levels[name] = new ReflectLevel (typePhys);
                this.levels[name].init();
                break;
            case "levelPi":
                this.levels[name] = new PiLevel (typeMath);
                this.levels[name].init();
                break;
            case "levelPythagoras":
                this.levels[name] = new PythagorasLevel (typeMath);
                this.levels[name].init();
                break;
            case "levelSort":
                this.levels[name] = new SortLevel (typeInf);
                this.levels[name].init();
                break;
            case "levelTurtle":
                this.levels[name] = new TurtleLevel (typeInf);
                break;
        }
        
    }
    
    function LoadLevels (){
        for (var key in this.bitmaps) {
            if (key == "each")
                break;
            this.bitmaps[key].removeAllEventListeners();
        }
        for (var i = 0; i < this.levelOrder.length;i++){
            this.LoadLevel(this.levelOrder[i]);
        }
    }

    function LoadJokeLevels (){
        for (var i = 0; i < 9;i++){
            this.jokeLevels[i] = new JokeLevel(jokes[i]);
        }
    }


    
    function GameInitialize () {
        this.LoadLevels();
        this.LoadJokeLevels();
        this.LoadMenus();
        this.LoadMainMenu ();
    }
    
    function LoadMenus(){
        this.menus["MainMenu"] = new MainMenu();
        this.menus["NextLevelMenu"] = new NextLevelMenu(); 
        this.menus["LevelMenu"] = new LevelMenu();
        this.menus["SelectedLevelWin"] = new SelectedLevelWin();
    }
    
    function LoadMainMenu (){
        while (this.numChildren > 0)
            this.removeChildAt(0);
        this.addChild (this.menus["MainMenu"]);
    }
    
    function StartLevel (level){
        while (this.numChildren > 0)
            this.removeChildAt(0);
        if (this.selectLevelMode){
            level.removeChild(level.skipButton);
            level.pauseButton.x = MainContainerWidth - MARGIN - level.pauseButton.width;
            level.restartButton.x = level.pauseButton.x - MARGIN - level.restartButton.width;
        }
        this.addChild (level);
    }
    
    function NextLevel (){
        this.currentLevel++;
        if (this.currentLevel<18){
            if (this.currentLevel%2 == 0){
                var next = Math.floor(this.currentLevel/2);
                this.StartLevel (this.levels[this.levelOrder[next]]);
            }
            else{
                var next = Math.floor(this.currentLevel/2);
                this.StartLevel (this.jokeLevels[next]);
            }
        }
        else{
            while (this.numChildren > 0)
                this.removeChildAt(0);
            this.addChild (new Graph(this.score));
        }
    }

    function RestartLevel (){
        var levelOrd = Math.floor(this.currentLevel/2);
        for (var key in this.bitmaps) {
            if (key == "each")
                break;
            this.bitmaps[key].removeAllEventListeners();
        }
        while (this.numChildren > 0)   
            this.removeChildAt(0);    
        this.levels[this.levelOrder[levelOrd]] = null;
        this.LoadLevel (this.levelOrder[levelOrd]);
        this.StartLevel (this.levels[this.levelOrder[levelOrd]]);      
    }
    
    
    function LoadBitmaps (imageLoader){
        for (var i = 0; i < imageLoader.imagePaths.length;i++){
            var reg = /picture{1}.*$/;
            var path = imageLoader.imagePaths[i].match(reg)[0];
            this.bitmaps [path] = new Bitmap(new BitmapData(imageLoader.loadedImages[path]));
        }
    }
}

